package com.dylan.datacollection;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dylan.datacollection.adapter.MsgListAdapter;
import com.dylan.datacollection.adapter.RankListAdapter;
import com.dylan.datacollection.entity.MsgEntity;
import com.dylan.datacollection.net.HttpConfig;
import com.dylan.datacollection.net.HttpHelper;
import com.dylan.datacollection.net.ResponseCallback;
import com.dylan.datacollection.utils.ToastUtils;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class MsgListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
    Context context;
    MsgListAdapter msgListAdapter;
    private PullToRefreshListView pullToRefreshListView = null;
    int pageNum = 1;
    final int LIST_VIEW_STATE_DEFAULT = 1;
    final int LIST_VIEW_STATE_LOADMORE = 2;
    List<MsgEntity> dataList = new ArrayList<MsgEntity>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_list);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        context = this;
        pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.msg_listView);
        pullToRefreshListView.setOnItemClickListener(this);
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNum = 1;
                getMsgList( LIST_VIEW_STATE_DEFAULT );
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageNum++;
                getMsgList(LIST_VIEW_STATE_LOADMORE );
            }
        });
        getMsgList(LIST_VIEW_STATE_DEFAULT);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish(); // back button
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void getMsgList(final int status){
        HttpHelper.getInstance().msgList(pageNum,new ResponseCallback(){
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
            }

            @Override
            public void onResponse(Object response, int id) {
                super.onResponse(response, id);
                try {
                    Log.i("msg","response:"+response.toString());
                    JSONObject jsonObject = new JSONObject(response.toString());
                    if (jsonObject.optInt("code") == HttpConfig.SUCCESS){
                        JSONArray jsonArr = jsonObject.optJSONArray("data");
                        List<MsgEntity> requestArrays = new ArrayList<MsgEntity>();
                        for( int i=0; i<jsonArr.length(); i++ ){
                            JSONObject jsonObj = jsonArr.getJSONObject(i);
                            Gson gson = new Gson();
                            MsgEntity msgEntity = gson.fromJson(new String(jsonObj.toString()),MsgEntity.class);
                            requestArrays.add(msgEntity);
                        }
                        if (requestArrays.isEmpty()){
                            pageNum--;
                        }
                        if (status == LIST_VIEW_STATE_DEFAULT){
                            dataList = requestArrays;
                            msgListAdapter = new MsgListAdapter(0,context,dataList);
                            pullToRefreshListView.setAdapter(msgListAdapter);
                        }else if(status == LIST_VIEW_STATE_LOADMORE){
                            dataList.addAll(requestArrays);
                            msgListAdapter.setmDatas(dataList,0);
                        }
                    }else{
                        ToastUtils.showShortMsg(context,jsonObject.optString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        MsgEntity msgEntity = (MsgEntity) parent.getItemAtPosition(position);
        Intent intent = new Intent(context,MsgDetailActivity.class);
        intent.putExtra("msg",msgEntity);
        startActivity(intent);
    }
}
